<!--  jquery validate  -->
// 登录验证
// 在键盘按下并释放及提交后验证
$(function () {
    $("#loginForm").validate({
        onsubmit: true, //提交时验证
        onfocusout:false,// 是否在获取焦点时验证
        onkeydown: true, // 敲击键盘后验证
        rules: {
            aaccount: {
                required: true,
                minlength: 10,
                digits: true
            },
            aemail: {
                required: true,
                maxlength: 32,
                email:true
            },
            apassword: {
                required: true,
                minlength: 6
            },
            pwd1:{
                required: true,
                minlength: 6,
                equalTo: "#apassword"
            }
        },
        errorPlacement: function (error, element) {
            error.appendTo(element.parent());
        },
        messages: {
            aaccount: {
                required: "请输入账号!",
                minlength: "账号最少10位!",
                digits: "账号必须为整数!",
                maxlength: "账号最多22位!"
            },
            aemail: {
                required: "请输入邮件!",
                maxlength: "邮箱最多32位!",
                email:"无效的邮件地址!"
            },
            apassword: {
                required: "请输入密码!",
                minlength: "密码最少6位!",
                maxlength: "账号最多12位!",
            },
            pwd1:{
                required: "请输入密码!",
                minlength: "密码最少6位!",
                equalTo: "密码不一致!"
            },
            yzm:{required:'请输入验证码',minlength:'请核对验证码',maxlength:'请核对验证码'},
        },
        submitHandler: function () { //验证通过 异步发送
            const login = $("#loginbtn").val();
            if (login == 0) { //登录
                layer.msg('正在登录',{icon:16,shade:0.01}); //加载样式
                var $input = $('input:checkbox[name="jzw"]:checked');
                var check = $input.length > 0 ? $input[0].value : 0 ;
                console.log(check);
                $.ajax({
                    type: "post",
                    url: "/account/login",
                    data: {
                        "aaccount":$("#aaccount").val(),
                        "apassword": $("#apassword").val(),
                        "check": check,
                        "yzm": $("#yzm").val()
                    },
                    success: function (resultMsg) {
                        if (resultMsg.state == 200 ) {
                            layer.msg(resultMsg.msg,{icon:1,time: 1000,end:function () {
                                    window.location.href="/";
                                }});
                        }  else if (resultMsg.state == 412) {
                            layer.msg(resultMsg.msg,{icon:0,time: 1500});
                            getCode();
                        } else if (resultMsg.state == 403) {
                            layer.confirm(resultMsg.msg,{
                                btn:['下线','取消'],
                            },function () {
                                layer.msg('正在退出,请稍后',{icon: 16,shade:0.01});
                                $.get('/account/loginout',{'num':1},function (resultMsg) {
                                    if (resultMsg.state == 200) {
                                        layer.msg(resultMsg.msg,{icon:6,time:1000});
                                    } else {
                                        layer.msg(resultMsg.msg,{icon:5,time:1000});
                                    }
                                })
                            },function () {
                                layer.msg('已取消',{icon:0,time:1000});
                            })
                            getCode();
                        }
                    },
                    error: function () {
                        layer.closeAll('loading')//取消加载样式
                        layer.msg('接口数据异常,请刷新页面重试！',{icon:2,time:1000});
                        getCode();
                    }
                });
            }
            if (login == 1) { //注册
                $.ajax({
                    type: "post",
                    url: "/account/register",
                    data: $("#loginForm").serialize(),
                    success: function (resultMsg) {
                        if (resultMsg.state == 201) {
                            layer.msg(resultMsg.msg, {
                                icon: 1, time: 1000, end: function () {
                                    window.location.href = "/navigation/login";
                                }
                            });
                        } else {
                            layer.msg(resultMsg.msg, {icon: 0, time: 1000,});
                            getCode();
                        }
                    },
                    error: function () {
                        layer.closeAll('loading')//取消加载样式
                        layer.msg('接口数据异常,请刷新页面重试！',{icon:2,time: 1000});
                        getCode();
                    }
                });
            }
        },
        invalidHandler:function (foem,validator) { //不通过回调
            return false;
        }
    });
});